<template>
    <div>
        <el-drawer v-model="showDrawer" 
            :title="title" 
            :size="size" 
            :destroy-on-close="destroyOnClose"
            :close-on-click-modal="closeOnClickModal">
            <div class="formDrawer">
                <div class="body">
                    <slot></slot>
                </div>
                <div class="actions">
                    <el-button class="mt-4 w-[120px]" type="primary" :loading="loading" @click="submit" >{{ confirmText }}</el-button>
                    <el-button class="mt-4 w-[120px]" type="default" @click="close">{{ cancelText }}</el-button>
                </div>
            </div>
        </el-drawer>
    </div>
</template>

<script setup>
import { ref } from 'vue'
const showDrawer = ref(false)
//加载状态
const loading = ref(false)
//暴露的属性
const props = defineProps({
    title: {
        type: String,
        default: "自定义标题"
    },
    size: {
        type: String,
        default: "45%"
    },
    destroyOnClose: {
        type: Boolean,
        default: false
    },
    closeOnClickModal: {
        type: Boolean,
        default: false
    },
    confirmText: {
        type: String,
        default: "提交"
    },
    cancelText: {
        type: String,
        default: "取消"
    }

})

// 显示加载状态
const showLoading = () => loading.value = true
// 隐藏加载状态
const hideLoading = () => loading.value = false
//打开
const open = () => showDrawer.value = true
//关闭
const close = () => showDrawer.value = false
// 暴露事件
const emit = defineEmits([
    "submit"
])
//提交事件
const submit = () => emit("submit")

//向父组件暴露方法
defineExpose({
    open,
    close,
    showLoading,
    hideLoading

})

</script>

<style>
.formDrawer {
    width: 100%;
    height: 100%;
    position: relative;
    @apply flex flex-col
}

.formDrawer .body {
    flex: 1;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 80px;
    overflow-y: auto;
}

.formDrawer .actions {
    height: 80px;
    @apply flex items-center mt-auto
}
</style>